package com.xbongbong.paas.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.service.WriteOffRecordService;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.CouponEntity;
import com.xbongbong.saas.domain.entity.WriteOffRecordEntity;
import com.xbongbong.saas.enums.WriteOffRecordCouponEnum;
import com.xbongbong.saas.enums.WriteOffRecordEnum;
import com.xbongbong.saas.enums.business.OrderEnum;
import com.xbongbong.saas.enums.dictionary.CouponTypeEnum;
import com.xbongbong.saas.enums.dictionary.OrderStatusEnum;
import com.xbongbong.saas.enums.dictionary.WriteOffStatusEnum;
import com.xbongbong.saas.model.WriteOffRecordModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("WriteOffRecordService")
public class WriteOffRecordServiceImpl implements WriteOffRecordService {


    @Resource
    private WriteOffRecordModel writeOffRecordModel;



    @Override
    public void insertRecord(PaasFormDataEntity newPaasFormDataEntity, List<CouponEntity> couponEntityList) {
        JSONObject orderData = newPaasFormDataEntity.getData();
        WriteOffRecordEntity writeOffRecordEntity = new WriteOffRecordEntity();
        writeOffRecordEntity.setOrderId(newPaasFormDataEntity.getId());
        writeOffRecordEntity.setOrderStatus(OrderStatusEnum.PENDING_ORDER_REVIEW.getCode());
        writeOffRecordEntity.setSerialNo(newPaasFormDataEntity.getSerialNo());
        writeOffRecordEntity.setDistributorId(orderData.getLong(OrderEnum.ORDERER_ID.getAttr()));
        writeOffRecordEntity.setCorpid(newPaasFormDataEntity.getCorpid());
        writeOffRecordEntity.setOrderTime(newPaasFormDataEntity.getAddTime());
        JSONObject data = new JSONObject();
        JSONArray couponArray = new JSONArray();
        for (CouponEntity couponEntity : couponEntityList) {
            JSONObject couponData = new JSONObject();
            couponData.put(StringConstant.SAAS_LINK_BUSINESS_ID, couponEntity.getId());
            couponData.put(WriteOffRecordCouponEnum.NAME.getAttr(), couponEntity.getName());
            couponData.put(WriteOffRecordCouponEnum.SERIAL_NO.getAttr(), couponEntity.getCouponCode());
            couponData.put(WriteOffRecordCouponEnum.TYPE.getAttr(), CouponTypeEnum.getByBusinessType(couponEntity.getBusinessType()).getCode());
            couponData.put(WriteOffRecordCouponEnum.USED_DATE.getAttr(), newPaasFormDataEntity.getAddTime());
            couponData.put(WriteOffRecordCouponEnum.STATUS.getAttr(), WriteOffStatusEnum.WRITTEN_OFF.getCode());
            couponArray.add(couponData);
        }
        data.put(WriteOffRecordEnum.COUPON.getAttr(), couponArray);
        writeOffRecordEntity.setData(data);
        writeOffRecordModel.insert(writeOffRecordEntity);
    }

    @Override
    public void updateOrderStatus(String status, Long orderId, String corpid) {
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("orderId", orderId);
        modelMap.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        List<WriteOffRecordEntity> entities = writeOffRecordModel.findEntities(modelMap);
        if (CollectionUtils.isNotEmpty(entities) && StringUtil.isNotEmpty(status)) {
            WriteOffRecordEntity writeOffRecord = entities.get(0);
            WriteOffRecordEntity writeOffRecordEntity = new WriteOffRecordEntity();
            writeOffRecordEntity.setOrderStatus(status);
            writeOffRecordEntity.setId(writeOffRecord.getId());
            writeOffRecordEntity.setCorpid(corpid);
            writeOffRecordModel.update(writeOffRecordEntity);
        }
    }

    @Override
    public void updateOrderIdsStatus(String status, List<Long> writeOffContractIds, String corpid) {
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("orderIdIn", writeOffContractIds);
        modelMap.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        List<WriteOffRecordEntity> entities = writeOffRecordModel.findEntities(modelMap);
        if (CollectionUtils.isNotEmpty(entities) && StringUtil.isNotEmpty(status)) {
            for (WriteOffRecordEntity entity : entities) {
                WriteOffRecordEntity writeOffRecordEntity = new WriteOffRecordEntity();
                writeOffRecordEntity.setOrderStatus(status);
                writeOffRecordEntity.setId(entity.getId());
                writeOffRecordEntity.setCorpid(corpid);
                writeOffRecordModel.update(writeOffRecordEntity);
            }
        }
    }
}
